﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Adjunct.Framework.Nuspec;

namespace Adjunct.Framework.NuGet
{
	public class PackagesConfig
	{
		private readonly List<NuspecDependency> _dependencies = new List<NuspecDependency>();

		public PackagesConfig(IEnumerable<NuspecDependency> dependencies = null)
		{
			if (dependencies != null)
			{
				_dependencies.AddRange(dependencies);
			}
		}

		public IEnumerable<NuspecDependency> GetDependencies(IEnumerable<string> excludeIds = null)
		{
			return (excludeIds == null)
			       	? _dependencies
			       	: (from d in _dependencies
			       	   where !excludeIds.Contains(d.Id.ToLowerInvariant())
			       	   select d);
		}

		public static PackagesConfig Load(string path)
		{
			return new PackagesConfig(from package in XElement.Load(path).Elements("package")
			                          let id = package.AttributeValue("id")
			                          let version = package.AttributeValue("version", null)
			                          select new NuspecDependency(id, version));
		}
	}
}